package com.googlecode.yoohoo.xmppcore.model.orm.account;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.yoohoo.xmppcore.model.account.AccountConflictException;
import com.googlecode.yoohoo.xmppcore.model.account.IAccount;
import com.googlecode.yoohoo.xmppcore.model.account.IAccountManager;
import com.googlecode.yoohoo.xmppcore.model.account.InvalidAccountException;

public class AccountManager implements IAccountManager {
	private List<IAccount> accounts;
	
	public AccountManager() {
		accounts = new ArrayList<IAccount>();
	}

	@Override
	public boolean exists(String name) {
		for (IAccount account : accounts) {
			if (account.getName().equals(name)) {
				return true;
			}
		}
		
		return false;
	}

	@Override
	public void create(IAccount account) throws AccountConflictException,
				InvalidAccountException {
		if (account.getName() == null) {
			throw new InvalidAccountException("Null account name.");
		}
		
		if (exists(account.getName())) {
			throw new AccountConflictException();
		}
		
		accounts.add(account);
	}

	@Override
	public IAccount getByName(String name) {
		for (IAccount account : accounts) {
			if (account.getName().equals(name)) {
				return account;
			}
		}
		
		return null;
	}

}
